#include <iostream>
#include <string>

#include "Basc_CarControlMcuCHSCANDDS_Skeleton.hpp"
#include "dds_server.hpp"
#include "Basc_ActSenMode_Skeleton.hpp"
#include "public_define.h"

using namespace com::changan::carControl::BasicService::skeleton;

void sendConditionCanId(Basc_CarControlMcuCHSCANDDS_Skeleton &skeleton, uint8_t *data)
{
    CAN2DDS_ARRAY_Struct stuct;
    stuct.seq._length = 11;
    stuct.seq._maximum = 11;
    stuct.seq._buffer = new uint8_t[stuct.seq._length];
    memcpy(stuct.seq._buffer, data, stuct.seq._length);
    stuct.seq._release = false;
    skeleton.Ntf_MCUCHSCANInfo.send(stuct);
}

void sendCycelCanId(Basc_CarControlMcuCHSCANDDS_Skeleton &skeleton, uint8_t first, uint8_t second)
{
    uint8_t _buffer1[] = {0x02, 0xD2, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    _buffer1[0] = first;
    _buffer1[1] = second;
    CAN2DDS_ARRAY_Struct stuct;
    stuct.seq._length = 11;
    stuct.seq._maximum = 11;
    stuct.seq._buffer = new uint8_t[stuct.seq._length];
    memcpy(stuct.seq._buffer, _buffer1, stuct.seq._length);
    stuct.seq._release = false;
    skeleton.Ntf_MCUCHSCANInfo.send(stuct);
}

void send(Basc_CarControlMcuCHSCANDDS_Skeleton &skeleton)
{
    //周期模拟

    sendCycelCanId(skeleton, 0x00, 0x50);
    sendCycelCanId(skeleton, 0x01, 0x83);
    sendCycelCanId(skeleton, 0x01, 0xC2);
    sendCycelCanId(skeleton, 0x01, 0x71);
    sendCycelCanId(skeleton, 0x02, 0x44);
    sendCycelCanId(skeleton, 0x02, 0xD2);
    sendCycelCanId(skeleton, 0x03, 0x56);
    sendCycelCanId(skeleton, 0x03, 0xE0);
    sendCycelCanId(skeleton, 0x05, 0xAE);

    //条件模拟
    uint8_t _buffer1[] = {0x00, 0x50, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    sendConditionCanId(skeleton, _buffer1);
    uint8_t _buffer2[] = {0x02, 0x88, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x26, 0x07, 0x08};
    sendConditionCanId(skeleton, _buffer2);
    std::cout << "send once" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "argc != 2." << std::endl;
        return 0;
    }
    std::string array = argv[1];
    // can data
    if (array == "1")
    {
        Basc_CarControlMcuCHSCANDDS_Skeleton skeleton(nullptr);
        skeleton.init();
        com::changan::rpc_service::DDSServer::register_service(&skeleton);
        com::changan::rpc_service::DDSServer::run();
        while (1)
        {
            send(skeleton);
            std::this_thread::sleep_for(std::chrono::milliseconds(5));
        }
    }
    else if (array == "2")
    { //哨兵模式 data
        using namespace com::changan::carCabin::BasicService::skeleton;
        Basc_ActSenMode_Skeleton skeleton(nullptr, global_data::g_can_data_dds_conf);
        skeleton.init();
        com::changan::rpc_service::DDSServer::register_service(&skeleton);
        com::changan::rpc_service::DDSServer::run();

        while (1)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            uint8_t code = 0x01;
            skeleton.Ntf_ActSenTrig.send(code);
            std::cout << "Basc_ActSenMode_Skeleton send " << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
        }
    }
    return 0;
}